package com.ntd.study.protocol.emq.core;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ntd.util.SslUtil;
import lombok.Data;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import javax.net.ssl.SSLSocketFactory;
import java.io.FileInputStream;

@Data
public class NtdMqttConfig {

    private String url;
    private String clientId;
    private String username;
    private String password;
    private MqttCallback mqttCallback;
    private MqttConnectOptions mqttConnectOptions;
    private MqttClientPersistence mqttClientPersistence;
    private String sslPath;
    private boolean initTag = false;

    public static NtdMqttConfig create(String url, String username, String password) {

        NtdMqttConfig ntdMqttConfig = new NtdMqttConfig();
        ntdMqttConfig.setUrl(url);
        ntdMqttConfig.setClientId(RandomUtil.randomString(10));
        ntdMqttConfig.setUsername(username);
        ntdMqttConfig.setPassword(password);

        return ntdMqttConfig;
    }

    public NtdMqttConfig init() {
        try {
            checkInitParam(this);
            initTag = true;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return this;
    }

    private static void checkInitParam(NtdMqttConfig ntdMqttConfig) throws IllegalAccessException {
        if (ObjectUtil.isNull(ntdMqttConfig.getMqttClientPersistence())) {
            ntdMqttConfig.setMqttClientPersistence(new MemoryPersistence());
        }
        if (ObjectUtil.isNull(ntdMqttConfig.getMqttCallback())) {
            ntdMqttConfig.setMqttCallback(new NtdMqttOnMsg());
        }
        if (ObjectUtil.isNull(ntdMqttConfig.getMqttConnectOptions())) {
            ntdMqttConfig.setMqttConnectOptions(defaultConnectOptions(ntdMqttConfig));
        }
    }

    /**
     * 获取连接参数
     * @return
     * @param ntdMqttConfig
     */
    private static MqttConnectOptions defaultConnectOptions(NtdMqttConfig ntdMqttConfig) throws IllegalAccessException {
        if (ObjectUtil.hasEmpty(ntdMqttConfig.getUsername(), ntdMqttConfig.getPassword())) {
            throw new IllegalAccessException("账号密码不能为空");
        }
        // MQTT 连接选项
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName(ntdMqttConfig.getUsername());
        connOpts.setPassword(ntdMqttConfig.getPassword().toCharArray());
        // 保留会话
        connOpts.setCleanSession(true);

        try {
            String sslPath = ntdMqttConfig.getSslPath();
            if (StrUtil.isNotBlank(sslPath)) {
                SSLSocketFactory singleSocketFactory = SslUtil.getSingleSocketFactory(new FileInputStream(sslPath));
                connOpts.setSocketFactory(singleSocketFactory);
//                connOpts.setSSLHostnameVerifier(new TrustAnyHostnameVerifier());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return connOpts;
    }

}